import axios from "axios";
import {ElNotification} from "element-plus";


const config = {
    // baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 5000
}
const instance = axios.create(config)

instance.interceptors.request.use(
    (config) => {
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)

instance.interceptors.response.use(
    (response) => {
        const isBlob = response.data instanceof Blob;
        if (isBlob) {
            return response
        }
        const {code, data, msg} = response.data
        if (code !== '200' ) {
            ElNotification({
                title: '错误1',
                message: msg,
                type: 'error',
            })
            return Promise.reject(msg)
        }
        //前后段约定，如果code为002则表示登录失效
        if (code === '002') {
            ElNotification({
                title: '错误',
                message: '登录失效',
                type: 'error',
            })
            setTimeout(() => {
                window.location.replace(`/login`);
            }, 500);
            return Promise.reject(msg)
        }
        return response.data
    },
    (error) => {
        return Promise.reject(error)
    }
)


function http(config:any) {
    return instance(config).then((res) => {
        return res
    })
}
export default http
